var t = require("../../../../@babel/runtime/helpers/typeof"), a = getApp(), e = null;

Page({
    data: {
        stop1: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/stop1.png",
        play1: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/play1.png",
        bfstatus: !1,
        currentTime: "0:00:00",
        slmin: "0:00:00",
        valueSlmin: 0,
        audio_length: 0,
        article_id: "",
        dataInfo: {
            audio_link: ""
        },
        isPlay: !1,
        isShow: 1,
        codeUrl: "",
        replyName: "",
        commentPage: 1,
        commentList: [],
        last_page: 1,
        commenMember: [],
        keyInd: 0,
        comment_id: 0,
        comment: !1,
        commentNum: 0,
        imgList: [],
        maincomment: "",
        replyContent: "",
        is_liked: 0
    },
    onLoad: function(t) {
        e = wx.createInnerAudioContext(), t.article_id && this.setData({
            article_id: t.article_id
        }), this.setData({
            options: t
        }), this.getData();
    },
    onReady: function() {},
    onShow: function() {
        this.init(), "article" == this.data.options.task_type && this.shareArticleTask();
    },
    shareArticleTask: function() {
        var t = {
            record_id: this.data.options.taskID,
            task_type: "article",
            day: this.data.options.taskDay,
            task_record_id: this.data.article_id
        }, e = a.getNetAddresss("plugin.task-package.frontend.daily-record.finish-task");
        a._postNetWork({
            url: e,
            showToastIn: !1,
            data: t,
            success: function(t) {},
            fail: function(t) {}
        });
    },
    onHide: function() {
        e.stop(), e.destroy();
    },
    onUnload: function() {
        e.stop(), e.destroy();
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        var t = wx.getStorageSync("yz_uid"), a = "";
        t && (a = t);
        var e = {
            path: "/packageA/member/course/VoiceDetail/VoiceDetail?article_id=" + this.data.article_id + "&mid=" + a,
            title: this.data.dataInfo.title,
            imageUrl: this.data.dataInfo.thumb ? this.data.dataInfo.thumb : ""
        };
        return this.data.options.taskID && (e.path += "&taskID=".concat(this.data.options.taskID, "&taskDay=").concat(this.data.options.taskDay, "&task_type=article")), 
        e;
    },
    audioPlay: function() {
        this.data.isPlay ? (this.data.isPlay = !1, this.innerAudioContext.pause()) : (this.data.isPlay = !0, 
        this.innerAudioContext.play());
    },
    toAdv: function() {
        console.log("跳转到广告"), !a._isTextEmpty(this.data.dataInfo) && a._isTextEmpty(this.data.dataInfo.min_app_advs_link) || wx.navigateTo({
            url: this.data.dataInfo.min_app_advs_link
        });
    },
    clickLike: function() {
        this._toLike();
    },
    toReport: function() {
        wx.navigateTo({
            url: "/packageA/member/article/noticeReport/noticeReport?article_id=" + this.data.article_id
        });
    },
    toLink: function(t) {
        var a = t.currentTarget.dataset.link;
        wx.navigateTo({
            url: a
        });
    },
    _toLike: function(t) {
        var e = this;
        a._getNetWork({
            url: a.getNetAddresss("plugin.article.api.article.like"),
            data: {
                article_id: e.data.article_id
            },
            success: function(a) {
                1 == a.data.result ? (1 == a.data.data.liked ? (e.setData({
                    is_liked: !0,
                    like_num: e.data.like_num + 1
                }), wx.showToast({
                    title: "点赞成功"
                })) : (e.setData({
                    is_liked: !1,
                    like_num: e.data.like_num - 1
                }), e.data.like_num < 0 && e.setData({
                    like_num: 0
                }), wx.showToast({
                    title: "取消点赞",
                    icon: "none"
                })), t && t(a)) : console.error(a);
            },
            fail: function(t) {
                console.error(t);
            }
        });
    },
    delComment: function(t) {
        var a;
        console.log(t);
        try {
            a = wx.getStorageSync("uid");
        } catch (t) {
            a = "";
        }
        var e, i = t.currentTarget.dataset.item, n = t.currentTarget.dataset.ind;
        try {
            e = t.currentTarget.dataset.index;
        } catch (t) {
            e = null;
        }
        console.log(i, n, e), this.data.commentList[n].child.length > 0 ? i.uid == a && (console.log("子评论是我的，可以删"), 
        this.delRequst(i.id, n, e, "child")) : a == i.uid && (console.log("主评论是我的，可以删"), 
        this.delRequst(i.id, n, e, "list"));
    },
    watchImg: function(t) {
        var a = t.currentTarget.dataset.item;
        wx.previewImage({
            current: a[0],
            urls: a
        });
    },
    replyBtn: function(t) {
        var a = t.currentTarget.dataset.item, e = a.nick_name;
        this.setData({
            keyInd: t.currentTarget.dataset.kind,
            replyshow: !0,
            replyId: a.id,
            comment_id: 0 == a.comment_id ? a.comment_id : a.id,
            replyName: e,
            commentManagement: !1
        });
    },
    moreCommentLis: function(t) {
        console.log(t), wx.showLoading({
            title: "加载中",
            icon: "none",
            duration: 1e3
        });
        var e = this, i = t.currentTarget.dataset.item.id, n = t.currentTarget.dataset.ind;
        console.log(this.data.commentList);
        var o = Number(this.data.commentList[n].commenMorePage), s = {
            page: o,
            comment_id: i
        };
        a._getNetWork({
            url: a.getNetAddresss("plugin.article.api.comment.get-replys"),
            data: s,
            success: function(t) {
                if (1 == t.data.result) {
                    var a = t.data.data;
                    console.log(a);
                    var i = a.data, s = e.data.commentList[n].reply_num - a.data.length;
                    e.data.commentList[n].reply_num = s;
                    var c = e.data.commentList[n].child.concat(i);
                    e.data.commentList[n].child = c;
                    var r = a.data.last_page;
                    o >= r ? o = 1 : o += 1, e.data.commentList[n].commenMorePage = o, e.setData({
                        commentList: e.data.commentList
                    });
                } else console.error(t);
                wx.hideLoading({
                    success: function(t) {}
                });
            },
            fail: function(t) {
                wx.hideLoading({
                    success: function(t) {}
                }), console.error(t);
            }
        });
    },
    getMore: function() {
        var t = this;
        this.setData({
            commentPage: this.data.commentPage + 1
        }), wx.showLoading({
            title: "加载中",
            duration: 1e3,
            icon: "none"
        }), a._getNetWork({
            url: a.getNetAddresss("plugin.article.api.comment.comment-list"),
            data: {
                page: this.data.commentPage,
                article_id: this.data.article_id
            },
            success: function(a) {
                if (1 == a.data.result) {
                    for (var e = a.data.data.list.data, i = 0; i < e.length; i++) e[i].reply_num > 0 ? e[i].commentShow = !0 : e[i].commentShow = !1, 
                    e[i].commenMorePage = 1, e[i].child = [];
                    t.setData({
                        commentList: t.data.commentList.concat(e),
                        last_page: a.data.data.list.last_page
                    });
                } else console.error(a);
                wx.hideLoading({
                    success: function(t) {}
                });
            },
            fail: function(t) {
                console.error(t), wx.hideLoading({
                    success: function(t) {}
                });
            }
        });
    },
    subMainCommentBtn: function() {
        this.setData({
            maincomment: "",
            mainCommentShow: !0
        });
    },
    delCommentIMg: function(t) {
        var a = t.target.dataset.index;
        this.data.imgList.splice(a, 1), this.setData({
            imgList: this.data.imgList
        });
    },
    subMainBtn: function(t) {
        var a = t.currentTarget.dataset.type;
        this.getReplyComments(a);
    },
    getReplyComments: function(t) {
        var e, i = this, n = {
            images: i.data.imgList,
            content: ""
        };
        if (console.log(t), "main" == t) {
            if (a._isTextEmpty(this.data.maincomment)) return void wx.showToast({
                title: "请输入评论内容",
                icon: "none",
                duration: 1e3
            });
            e = "plugin.article.api.comment.create-comment", n.article_id = i.data.article_id, 
            n.content = i.data.maincomment;
        } else if ("reply" == t) {
            if (a._isTextEmpty(this.data.replyContent)) return void wx.showToast({
                title: "请输入评论内容",
                icon: "none",
                duration: 1e3
            });
            e = "plugin.article.api.comment.reply-comment", n.content = i.data.replyContent, 
            n.comment_id = i.data.replyId;
        }
        wx.showLoading({
            title: "加载中",
            duration: 1e3,
            icon: "none"
        }), a._getNetWork({
            url: a.getNetAddresss(e),
            data: n,
            success: function(a) {
                1 == a.data.result ? "main" == t ? (i.setData({
                    commentPage: 1,
                    mainCommentShow: !1,
                    replyshow: !1,
                    maincomment: ""
                }), i.getComment()) : "reply" == t && i.updateInfo(a.data.data, i.data.keyInd) : console.error(a), 
                wx.hideLoading({
                    success: function(t) {}
                });
            },
            fail: function(t) {
                wx.hideLoading({
                    success: function(t) {}
                }), console.error(t);
            }
        });
    },
    updateInfo: function(t, a) {
        console.log(t, a);
        var e = [];
        e.push(t), this.data.commentList[a].child ? this.data.commentList[a].child.unshift(e[0]) : this.data.commentList[a].child = e, 
        this.setData({
            commentList: this.data.commentList,
            mainCommentShow: !1,
            replyshow: !1,
            replyContent: ""
        });
    },
    uploaderFun: function(t) {
        wx.showLoading({
            title: "加载中"
        });
        var e = this, i = a.getNetAddresss("upload.uploadPic");
        wx.chooseImage({
            count: 1,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(t) {
                var a = t.tempFilePaths[0];
                wx.uploadFile({
                    url: i,
                    filePath: a,
                    name: "file",
                    success: function(t) {
                        var a = JSON.parse(t.data);
                        if (wx.hideLoading(), 1 == a.result) {
                            var i = e.data.imgList;
                            i.push(a.data.img_url), e.setData({
                                imgList: i
                            });
                        } else wx.showToast({
                            icon: "none",
                            title: a.msg,
                            duration: 1500
                        });
                    }
                });
            },
            fail: function(t) {
                wx.hideLoading({
                    success: function(t) {}
                });
            }
        });
    },
    replyshowClose: function() {
        this.setData({
            replyshow: !1
        });
    },
    mainCommentShowClose: function() {
        this.setData({
            mainCommentShow: !1
        });
    },
    maincommentinp: function(t) {
        var a = t.detail.value;
        this.setData({
            maincomment: a
        });
    },
    replyContentinp: function(t) {
        var a = t.detail.value;
        this.setData({
            replyContent: a
        });
    },
    getData: function() {
        var t = this, i = this, n = a.getNetAddresss("plugin.article.api.article.get-article");
        n += "&article_id=" + this.data.article_id, n += "&share_uid=", a._getNetWork({
            url: n,
            success: function(a) {
                var n = a.data;
                1 == n.result ? (n.data.audio_link = decodeURIComponent(n.data.audio_link), i.setData({
                    dataInfo: n.data,
                    is_liked: n.data.liked,
                    like_num: n.data.like_num + n.data.virtual_like_num,
                    read_num: n.data.read_num + n.data.virtual_read_num,
                    codeUrl: n.data.miQrCodeUrl
                }), e.src = encodeURI(t.data.dataInfo.audio_link), wx.setNavigationBarTitle({
                    title: i.data.dataInfo.title ? i.data.dataInfo.title : ""
                }), n.data.comment && (i.setData({
                    comment: n.data.comment
                }), i.getComment())) : (i.setData({
                    isShow: 0
                }), "您尚未购买该文章内容,无法观看" == n.data.msg ? wx.showModal({
                    content: n.data.msg,
                    showCancel: !1,
                    success: function() {
                        wx.redirectTo({
                            url: "/packageD/article/articleList/articleList?article_id=" + i.data.article_id
                        });
                    }
                }) : wx.showModal({
                    content: n.data.msg,
                    showCancel: !1,
                    success: function() {
                        wx.navigateBack({
                            delta: 1
                        });
                    }
                }));
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getComment: function() {
        var t = this;
        a._getNetWork({
            url: a.getNetAddresss("plugin.article.api.comment.comment-list"),
            data: {
                page: this.data.commentPage,
                article_id: this.data.article_id
            },
            success: function(a) {
                if (1 == a.data.result) {
                    for (var e = a.data.data.list.data, i = 0; i < e.length; i++) e[i].reply_num > 0 ? e[i].commentShow = !0 : e[i].commentShow = !1, 
                    e[i].commenMorePage = 1, e[i].child = [];
                    t.setData({
                        last_page: a.data.data.list.last_page,
                        commenMember: a.data.data.member,
                        commentList: a.data.data.list.data,
                        commentNum: a.data.data.num
                    });
                } else console.error(a);
            },
            fail: function(t) {
                console.error(t);
            }
        });
    },
    tapbf: function(t) {
        this.data.bfstatus ? e.pause() : (e.src = encodeURI(this.data.dataInfo.audio_link), 
        e.play()), this.setData({
            bfstatus: !this.data.bfstatus
        });
    },
    init: function() {
        var t = this;
        e.onCanplay(function() {}), e.onPlay(function() {
            console.log("开始播放"), console.log(e.duration), t.setData({
                currentTime: t.realFormatSecond(e.duration),
                slmin: "0:00:00"
            });
        }), e.onTimeUpdate(function() {
            setTimeout(function() {
                var a = e.currentTime / e.duration * 100, i = parseInt(a);
                t.setData({
                    currentTime: t.realFormatSecond(e.duration),
                    slmin: t.realFormatSecond(e.currentTime),
                    valueSlmin: i,
                    audio_length: e.duration
                });
            }, 1e3);
        }), e.onEnded(function(a) {
            console.log("播放结束"), t.setData({
                valueSlmin: 0,
                slmin: "0:00:00",
                bfstatus: !1
            });
        }), e.onError(function(t) {
            wx.showToast({
                title: "播放错误",
                icon: "none"
            });
        });
    },
    sildein: function(t) {
        var a = t.detail.value / 100, i = this.data.audio_length * a;
        console.log(i, a), e.seek(i);
    },
    realFormatSecond: function(a) {
        var e = t(a);
        if ("number" === e || "string" === e) {
            a = parseInt(a);
            var i = Math.floor(a / 3600);
            a -= 3600 * i;
            var n = Math.floor(a / 60);
            return a -= 60 * n, i + ":" + ("0" + n).slice(-2) + ":" + ("0" + a).slice(-2);
        }
        return "0:00:00";
    },
    getQrCode: function() {
        var t = this, e = a.getNetAddresss("member.member.getArticleQr");
        a._getNetWork({
            url: e,
            success: function(a) {
                var e = a.data;
                1 == e.result ? t.setData({
                    codeUrl: e.data
                }) : console.log(e.msg);
            },
            fail: function(t) {
                console.log(t);
            }
        });
    }
});